﻿

//========================常量命名变量，请不要更改此部分数据
var divAjaxAutoBoxClass = "divAjaxAutoBox";
var tbAjaxDataBoxClass = "tbAjaxDataBox";

var divAjaxAutoBoxPrefix = "jAjax_divAutoBox_";
var tbAjaxDataBoxPrefix = "jAjax_tbDataBox_";
//=========================================================



$(document).ready(function() {
    $(".txtAjaxPortAutoComplete").bind('keyup').keyup(function (e) {
        var key = (e.keyCode) || (e.which) || (e.charCode); //兼容IE(e.keyCode)和Firefox(e.which)
        if (key != 38 && key != 40 && key != 13 && key != 9) {
            doAjaxRequest1($(this));
        }
    })
    $(".txtAjaxPortAutoComplete").bind("focus",ShowSearchResult);
    $(".txtAjaxPortAutoComplete").bind("blur", CheckInputResult);
    
    //AJAX传输Loading显示
    $("#divLoading").ajaxStart(function() 
    { 
        $(this).show();
        
    }).ajaxStop(function() 
    { 
       $(this).hide();
    }); 
    
})


//判断是否发送AJAX请求
function PrepareAjaxRequest($text) {

    if (($text).type == 'keyup') {
        doAjaxRequest1($(this));
    }
}


//发送Ajax请求
function doAjaxRequest1($text)
{
    //提交字符串的转义字符处理
    var txtValue = $text.val();
    var postData = '{code:"' + txtValue + '"}';
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "../AjaxCallbackService.asmx/GetBaseCodePORTCD",
        data: postData,
        dataType: 'json',
        success: function(result) 
        {
            CreateResultDataBoxHTML($text,result);       
        },
        error: function(httpException) 
        {
            alert("数据加载失败，请重试2。");
        },
        timeout: 40000
    });
}

//根据AJAX请求结果生成的数据表单并弹出
function CreateResultDataBoxHTML($text, result)
{
    var divAutoBoxID = divAjaxAutoBoxPrefix + $text.attr("id");
    if ($("#" + divAutoBoxID).length == 0)
    {
        var divAutoBoxHTML = "<div id='" + divAutoBoxID + "' class='" + divAjaxAutoBoxClass + "' style='display:none;'></div>";
        $text.after(divAutoBoxHTML);
    }
    else
    {
        $("#" + divAutoBoxID).empty();
    }
    
    var tbDataBoxID = tbAjaxDataBoxPrefix + $text.attr("id");
    var tbDataBoxHTML = "<table cellspacing='0' cellpadding='0' id='" + tbDataBoxID + "' class='"  + tbAjaxDataBoxClass +  "'>";
    if (result.d.length == 0)
    {
        tbDataBoxHTML  = tbDataBoxHTML + "<tr><td></td></tr></table>";
    }
    else 
    {
        $(result.d).each(function() {
        
            var trHTML = "<tr class='trDataRow'><td class='tdText'>"+ this["EngValue"] + "&nbsp;<font color='blue'>[" +  this["Code"] +  "]</font></td>";
            trHTML = trHTML + "<td class='tdCode' style='display:none'>"+ this["Code"] + "</td>";
            trHTML = trHTML + "<td class='tdValue' style='display:none'>"+ this["EngValue"] + "</td></tr>";
            
            tbDataBoxHTML = tbDataBoxHTML + trHTML;
        });
        tbDataBoxHTML  = tbDataBoxHTML + "</table>";
    }
    
    var divAutoBoxID   = divAjaxAutoBoxPrefix + $text.attr("id");
    
    //将数据加入到弹出提示框中
    $("#" + divAutoBoxID).append(tbDataBoxHTML);

     //弹出提示框的位置设置
    $("#" + divAutoBoxID).css({width:$text.css("width") });
    $("#" + divAutoBoxID).css({top: (+ $text.css("height").replace("px","") + $text.offset().top + 6) , left: ($text.offset().left + 3) + "px" });
    $("#" + divAutoBoxID).show().slideDown("slow");
    
    
     var selecotr = "#" + tbDataBoxID + " tr.trDataRow" ;
    //绑定鼠标移动时间，设置鼠标移动行颜色      
    $(selecotr).mouseover(function()
    {
        $(selecotr).removeClass("trAjaxMouseover");
        $(this).addClass("trAjaxMouseover");
    })
    
    //绑定单击事件，设置输入框的值
    $(selecotr).click(function()
    {
        $(selecotr).removeClass("trAjaxSelected");
        $(this).addClass("trAjaxSelected");  
        var selectedCode = $($(this).find(".tdCode")[0]).text();
        var selectedValue = $($(this).find(".tdValue")[0]).text();
        
        $("#" + $text.attr("id")+"Code").val(selectedCode);
        $text.val(selectedValue);
    })
    //绑定键盘事件
    $($text).unbind('keydown').keydown(function (e) {
        var key = (e.keyCode) || (e.which) || (e.charCode); //兼容IE(e.keyCode)和Firefox(e.which)
        if (key == 38) {
            //alert("38");
            $(selecotr).eq(getIndexUp(tbDataBoxID)).addClass("trAjaxMouseover");
        } else if (key == 40) {
            $(selecotr).eq(getIndexDown(tbDataBoxID)).addClass("trAjaxMouseover");
        } else if (key == 13) {
            var len = $("#" + tbDataBoxID + " tr").size();
            for (var i = 0; i < len; i++) {
                if ($(selecotr).eq(i).hasClass("trAjaxMouseover")) {
                    var selectedCode = $("#" + tbDataBoxID + " tr:eq(" + i + ") td:eq(1)").text();
                    var selectedValue = $("#" + tbDataBoxID + " tr:eq(" + i + ") td:eq(2)").text();
                    $("#" + $text.attr("id") + "Code").val(selectedCode);
                    $text.val(selectedValue);
                    var divAutoBoxID = divAjaxAutoBoxPrefix + $(this).attr("id");
                    $("#" + divAutoBoxID).slideUp("fast");
                }
            }
        }
    })
    $("#" + divAutoBoxID).bgiframe();
                 
}

//文本框聚焦时的处理
function ShowSearchResult()
{
    var $text =  $(this);
    //第一次加载frmBookingProfile页面时的处理
    var divAutoBoxID = divAjaxAutoBoxPrefix + $text.attr("id");

    if ($("#" + divAutoBoxID).length==0)
    {
        PrepareAjaxRequest($text);
    }
    else 
    {
        //弹出页面
        $("#" + divAutoBoxID).show().slideDown("slow");
    }
}

//获取当前的索引,向下
function getIndexDown(id) {
    var index = 0;
    var len = $("#" + id + " tr").size();
    for (var i = 0; i < len; i++) {
        if ($("#" + id + " tr.trDataRow").eq(i).hasClass("trAjaxMouseover")) {
            $("#" + id + " tr.trDataRow").eq(i % len).removeClass("trAjaxMouseover");
            index = i + 1;
        }
    }
    return index > (len - 1) ? 0 : (index % len);
}
//获取当前的索引,向上
function getIndexUp(id) {
    var len = $("#" + id + " tr").size();
    var index = len - 1;
    for (var i = 0; i < len; i++) {
        if ($("#" + id + " tr.trDataRow").eq(i).hasClass("trAjaxMouseover")) {
            $("#" + id + " tr.trDataRow").eq(i % len).removeClass("trAjaxMouseover");
            index = i - 1;
        }
    }
    return (index < 0) ? (len - 1) : (index % len);
}
function CheckInputResult()
{
    //输入框失去焦点后，隐藏数据框
    var divAutoBoxID   = divAjaxAutoBoxPrefix + $(this).attr("id");
    $("#" + divAutoBoxID).slideUp("fast");
}
